package wcpackage.webapplication.view.users;

import org.apache.wicket.extensions.markup.html.repeater.data.table.*;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.TextFilteredPropertyColumn;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import wcpackage.domain.entities.User;
import wcpackage.service.UserService;
import wcpackage.webapplication.view.BasePage;

/**
 * User: rabbit
 * Date: Jun 3, 2009
 * Time: 10:51:34 PM
 */
public class UsersPage extends BasePage {


    @SpringBean
    private UserService userService;

    private User user;

    public UsersPage() {
        addUsersDataTable();
        addUsersAddForm();
    }

    private void addUsersDataTable() {
        IColumn<User>[] columns = new IColumn[3];

        columns[0] = new PropertyColumn<User>(new Model("Login"), "login", "login");
        columns[1] = new PropertyColumn<User>(new Model("First Name"), "firstName", "firstName");
        columns[2] = new TextFilteredPropertyColumn<User, String>(new Model("Last Name"), "lastName", "lastName");

        UsersProvider usersProvider = new UsersProvider(userService);
        DataTable<User> users = new DataTable<User>("users", columns, usersProvider, 10);
        users.addBottomToolbar(new NavigationToolbar(users));
        users.addTopToolbar(new HeadersToolbar(users, usersProvider));
        FilterForm<User> filterForm = new FilterForm<User>("filterForm", usersProvider);
        users.addTopToolbar(new FilterToolbar(users, filterForm, usersProvider));
        filterForm.add(users);
        add(filterForm);
    }

    private void addUsersAddForm() {
        user = new User();

        Form form = new Form("form", new CompoundPropertyModel(user));
        form.add(new TextField("login"));
        form.add(new TextField("firstName"));
        form.add(new TextField("lastName"));
        form.add(new Button("addButton") {
            @Override
            public void onSubmit() {
                userService.createNewUser(user);
            }
        });
        add(form);
    }
}